//
//  JPComposeView.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 2016/10/3.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit
import pop


class JPComposeView: UIView {

    //  目标控制器
    var target: UIViewController?
    
    //  口号图片
    private lazy var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    //  按钮数组
    private lazy var buttonArr: [JPComposeButton] = [JPComposeButton]()
    //  按钮模型
    private lazy var buttonList: [JPComposeMenu] = {
        //  获取plist文件的路径
        let path = Bundle.main.path(forResource: "compose.plist", ofType: nil)!
        //  获取到plist文件里面的数据
        let dicArray = NSArray(contentsOfFile: path)!
        let modelArr = NSArray.yy_modelArray(with: JPComposeMenu.self, json: dicArray)
        
        return modelArr as! [JPComposeMenu]
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupBotton()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //        let image = UIImage.screenShot().applyDarkEffect()
        let blur = UIBlurEffect.init(style: .light)
        
        let effectView = UIVisualEffectView.init(effect: blur)
        effectView.frame = self.bounds
        
        self.addSubview(effectView)
        self.addSubview(sloganImageView)
        
        sloganImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        
    }
    
    private func setupBotton() {
        //  按钮的宽度
        let buttonWidth: CGFloat = 80
        //  按钮的高度
        let buttonHeight: CGFloat = 110
        //  按钮的间距
        let buttonMargin: CGFloat = (width - 3 * buttonWidth) / 4
        
        for i in 0..<buttonList.count {
            //  所在的行
            let row = i / 3
            //  所在的列
            let col = i % 3
            
            let button = JPComposeButton()
            button.buttonModel = buttonList[i]
            button.x = CGFloat(col + 1) * buttonMargin + CGFloat(col) * buttonWidth
            button.y = CGFloat(row) * (buttonMargin + buttonHeight) + ScreenHeight
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            button.addTarget(self, action: #selector(buttoAction(btn:)), for: .touchUpInside)
            
            addSubview(button)
            
            buttonArr.append(button)
        }
    }
    
    @objc private func buttoAction(btn: JPComposeButton) {
        UIView.animate(withDuration: 0.25, animations: { 
            for button in self.buttonArr {
                if button == btn {
                    button.transform = CGAffineTransform(scaleX: 2, y: 2)
                }else {
                    button.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                    button.alpha = 0.2
                }
                
            }
            
            }) { (_) in
                UIView.animate(withDuration: 0.25, animations: { 
                    for button in self.buttonArr {
                        button.alpha = 1
                        button.transform = CGAffineTransform.identity
                    }
                    }, completion: { (_) in
                        let classType = NSClassFromString((btn.buttonModel?.className)!) as! UIViewController.Type
                        let vc = classType.init()
                        let nav = UINavigationController(rootViewController: vc)
                        self.target?.present(nav, animated: true, completion: {
                            self.removeFromSuperview()
                })
            })
        }
    }
    
    private func startPopAnimation(isUp: Bool) {
        if isUp == false {
            buttonArr = buttonArr.reversed()
        }
        
        for (index,button) in buttonArr.enumerated() {
            
            let popAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            
            if isUp {
                popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: button.centerX, y: button.centerY - 350))
            }else {
                popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: button.centerX, y: button.centerY + 350))
            }
            
            popAnimation?.springSpeed = 10
            popAnimation?.springBounciness = 10
            popAnimation?.beginTime = CACurrentMediaTime() + 0.025 * Double(index)
            
            button.pop_add(popAnimation, forKey: nil)

        }
        
    }
    
    func show(target: UIViewController) {
        self.target = target
        
        target.view.addSubview(self)
        
        startPopAnimation(isUp: true)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        startPopAnimation(isUp: false)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
            self.removeFromSuperview()
        }

    }
    
}
